
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Andromeda.Global.Business;
using Andromeda.Global.Data.Db;
using Andromeda.Global.Common;
using ForgeIt.Dal.dbo.Validator;

  
namespace ForgeIt.Dal.dbo.Stub
{
	internal abstract class DBEntityStub : dboSchema
	{
		#region private fields

		/// <summary>
		/// (fieldname, datatype, ischangeable(!isidentity), isinsertable(!isidentity), isforeignkey, foreigntable
		/// </summary>

		protected readonly DbField foIdEntity = new DbField("IdEntity", DbType.Int32, true, true, false, false, false, "");

		protected readonly DbField foName = new DbField("Name", DbType.String, false, false, true, true, false, "");

		protected readonly DbField foNickname = new DbField("Nickname", DbType.String, false, false, true, true, false, "");

		protected readonly DbField foImageBinary = new DbField("ImageBinary", DbType.Binary, false, false, true, true, false, "");

		protected readonly DbField foCreatedAt = new DbField("CreatedAt", DbType.DateTime, false, false, true, true, false, "");

		protected readonly DbField foModifiedAt = new DbField("ModifiedAt", DbType.DateTime, false, false, true, true, false, "");

        #endregion private fields

        #region constructors

        protected DBEntityStub()
		{
		}

		protected DBEntityStub(bool ignorethis, string foName, string foNickname, byte[] foImageBinary, System.DateTime foCreatedAt, System.DateTime foModifiedAt)
		{

			this.foName.Value = foName;

			this.foNickname.Value = foNickname;

			this.foImageBinary.Value = foImageBinary;

			this.foCreatedAt.Value = foCreatedAt;

			this.foModifiedAt.Value = foModifiedAt;

		}
	
		#endregion constructors
    
		#region public properties
		
		public override DbConnector CleanEntity
		{
			get { return new DBEntity(); }
		}


		public int IdEntity
		{
			get { return ObjectConverter.ToInt32(foIdEntity.Value); }
		}

		public string Name
		{
			get { return ObjectConverter.ToString(foName.Value); }
			set { foName.Value = value; }
		}

		public string Nickname
		{
			get { return ObjectConverter.ToString(foNickname.Value); }
			set { foNickname.Value = value; }
		}

		public byte[] ImageBinary
		{
			get { return ObjectConverter.ToByteArray(foImageBinary.Value); }
			set { foImageBinary.Value = value; }
		}

		public System.DateTime CreatedAt
		{
			get { return ObjectConverter.ToDateTime(foCreatedAt.Value); }
			set { foCreatedAt.Value = value; }
		}

		public System.DateTime ModifiedAt
		{
			get { return ObjectConverter.ToDateTime(foModifiedAt.Value); }
			set { foModifiedAt.Value = value; }
		}

  
		public override string TableName
		{
			get { return "Entity"; }
		}




		    protected override ValidationModelBase Validator
		    {
			    get { return new DBEntityValidator(); }
		    }
		#endregion public properties
    
		#region enumerator

		/// <summary>
		/// This enumerator contains a list of all the fields of the datasource
		/// </summary>
		public enum FieldNames
		{
			IdEntity,
			Name,
			Nickname,
			ImageBinary,
			CreatedAt,
			ModifiedAt,

		}

		#endregion enumerator
    
		#region generic field methods

		/// <summary>
		/// Validates if a given  field value is a DbNull
		/// </summary>
		/// <param name="fieldName">The field to validate</param>
		///<returns>true if is DbNull, false otherwise</returns>
		public bool IsDbNull(FieldNames fieldName)
		{
			switch (fieldName)
			{

				case FieldNames.IdEntity: return (foIdEntity.Value is DBNull);

				case FieldNames.Name: return (foName.Value is DBNull);

				case FieldNames.Nickname: return (foNickname.Value is DBNull);

				case FieldNames.ImageBinary: return (foImageBinary.Value is DBNull);

				case FieldNames.CreatedAt: return (foCreatedAt.Value is DBNull);

				case FieldNames.ModifiedAt: return (foModifiedAt.Value is DBNull);

			}

			throw new InvalidDataException();
		}

		/// <summary>
		/// Sets the given field value to DbNull
		/// </summary>
		/// <param name="fieldName">The field to set</param>
		public void SetDbNull(FieldNames fieldName)
		{
			switch (fieldName)
			{

				case FieldNames.IdEntity:
					foIdEntity.Value = DBNull.Value; break;

				case FieldNames.Name:
					foName.Value = DBNull.Value; break;

				case FieldNames.Nickname:
					foNickname.Value = DBNull.Value; break;

				case FieldNames.ImageBinary:
					foImageBinary.Value = DBNull.Value; break;

				case FieldNames.CreatedAt:
					foCreatedAt.Value = DBNull.Value; break;

				case FieldNames.ModifiedAt:
					foModifiedAt.Value = DBNull.Value; break;

			}
		}
		
		#endregion generic field methods
    
        #region static factories

		/// <summary>
		/// Creates a new DB object that can be added to the database as a new row
		/// </summary>
		/// <returns>The instance of the DB Object</returns>
		public static DBEntity NewRow
		{
			get { return new DBEntity(); }
		}

		/// <summary>
		/// Returns multiple rows from the Datasource by a given set of WHERE clause fields, only includes fields where value != null
		/// </summary>
		/// <example>List of fields to filter by, null to ignore field (null = all).</example>
		/// <returns>A collection with the filtered results. </returns>
		internal static IEnumerable<DBEntity> ListEntity(object foIdEntity, object foName, object foNickname, object foImageBinary, object foCreatedAt, object foModifiedAt)
		{
			DBEntity helper = new DBEntity();
			

			helper.foIdEntity.Value = foIdEntity;

			helper.foName.Value = foName;

			helper.foNickname.Value = foNickname;

			helper.foImageBinary.Value = foImageBinary;

			helper.foCreatedAt.Value = foCreatedAt;

			helper.foModifiedAt.Value = foModifiedAt;


			return helper.GetEntityInstancesList(helper.Fields.ToArray()).Cast<DBEntity>();
        }

		/// <summary>
		/// Returns multiple rows from the Datasource by a given set of WHERE clause fields, only includes fields where value != null
		/// </summary>
		/// <example>List of fields to filter by, null to ignore field (null = all).</example>
		/// <returns>A collection with the filtered results. </returns>
		internal static IEnumerable<DBEntity> ListEntity()
		{
			return new DBEntity().GetEntityInstancesList(new DbField[0]).Cast<DBEntity>();
        }

      		/// <summary>
		/// Returns a single row from the datasource by a given set of RUID strong-typed values
		/// </summary>
		/// <returns>The instance for this key or a new one if no match is found.</returns>
		internal static DBEntity GetEntity(int foIdEntity)
		{
			DBEntity helper = new DBEntity();

			helper.foIdEntity.Value = foIdEntity;

			helper = helper.GetEntityInstance(helper.Ruid) as DBEntity;
			
			return helper;
		}


		#endregion static factories
	}
}